﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using WindowsPhone.Tools;
using Microsoft.SmartDevice.Connectivity;

namespace EzDeploy
{
    class DeviceConnectionControl
    {
        private WindowsPhoneDevice _device;
        public WindowsPhoneDevice Device
        {
            get
            {
                return _device;
            }
            set
            {
                _device = value;
            }
        }

        private string wpconnect_Path
        {
            get { return Properties.Settings.Default.wpconnect_Path; }
            set
            {
                Properties.Settings.Default.wpconnect_Path = value;
                Properties.Settings.Default.Save();
            }
        }
        private string zune_Path
        {
            get { return Properties.Settings.Default.zune_Path; }
            set
            {
                Properties.Settings.Default.zune_Path = value;
                Properties.Settings.Default.Save();
            }
        }

        public DeviceConnectionControl(Device device)
        {
            _device = new WindowsPhoneDevice();
            _device.CurrentDevice = device;
        }

        public DeviceConnectionControl()
        {
        }


        public event EventHandler ShowStatusMessageHandler;
        public void ShowStatusMessage(string message)
        {
            ShowStatusMessageHandler(message, EventArgs.Empty);
        }

        public void test()
        {
            ShowStatusMessage("Connection");
        }

        public void Connect()
        {
            ShowStatusMessage(LocalizedStrings.str_connecting + " " + _device.CurrentDevice.Name + "...");
            _device.Connect();
            ShowStatusMessage(_device.StatusMessage);
            if (!_device.Connected)
            {
                ShowStatusMessage(LocalizedStrings.str_failedinconnecting + " " + _device.CurrentDevice.Name);

                if (EzDeploy.Properties.Settings.Default.auto_Wpconnect == true)
                {
                    connectViaWpc();
                }
            }
        }

        private void connectViaWpc()
        {
            bool isWpconnectOk = false;
            if (wpconnect_Path == "")
            {
                ShowStatusMessage(LocalizedStrings.str_wpconnectpathnotdefine);
                initiateWpconnectPath();
            }
            else
            {
                if (IsZuneRunning() == false)
                {
                    ShowStatusMessage(LocalizedStrings.str_launchingwpc + "...");
                    try
                    {
                        ShowStatusMessage(LocalizedStrings.str_waitingforwpc);
                        Process p = new Process()
                        {
                            StartInfo =
                            {
                                FileName = wpconnect_Path,
                                UseShellExecute = false,
                                CreateNoWindow = true,
                            }
                        };
                        p.Start();
                        p.WaitForExit();
                        if (p.ExitCode.ToString() == "0")
                        {
                            isWpconnectOk = true;
                            ShowStatusMessage(LocalizedStrings.str_wpconnectconnected + "!");
                        }
                        else
                        {
                            ShowStatusMessage(LocalizedStrings.str_wpconnectnotconnected + "!");
                        }
                    }
                    catch (Exception ex)
                    {
                        ShowStatusMessage(ex.Message.ToString());
                        wpconnect_Path = "";
                        initiateWpconnectPath();
                    }
                }
                else
                {
                    ShowStatusMessage(LocalizedStrings.str_wpconnectwontlaunch);
                }
            }

            if (isWpconnectOk && !IsZuneRunning())
            {
                ShowStatusMessage(LocalizedStrings.str_connectingviawpc + "...");
                _device.Connect();
                ShowStatusMessage(_device.StatusMessage);
            }
        }

        private bool IsZuneRunning()
        {
            bool result=false;
            foreach (Process singleProc in Process.GetProcesses())
            {
                if (singleProc.ProcessName.ToLower() == "zune")
                {
                    ShowStatusMessage("Zune is running");
                    result = true;
                    break;
                }
            }
            return result;
        }

        private void initiateWpconnectPath()
        {
            if (wpconnect_Path == "")
            {
                if (Tools.Detect3264() == "64")
                {
                    wpconnect_Path = @"C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.1\Tools\WPConnect\x64\WPConnect.exe";
                }
                else
                {
                    wpconnect_Path = @"C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.1\Tools\WPConnect\x86\WPConnect.exe";
                }
            }
        }

        private void initiateZunePath()
        {
            if (zune_Path == "")
            {
                zune_Path = @"C:\Program Files\Zune\Zune.exe";
            }
        }
    }
}
